package cn.iocoder.yudao.module.ad.service.channel;

import cn.iocoder.yudao.module.ad.framework.core.channel.AdChannelClientFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ad.controller.admin.channel.vo.*;
import cn.iocoder.yudao.module.ad.dal.dataobject.channel.AdChannelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ad.dal.mysql.channel.AdChannelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ad.enums.ErrorCodeConstants.*;

/**
 * 广告渠道 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class AdChannelServiceImpl implements AdChannelService {

    @Resource
    private AdChannelMapper adChannelMapper;


    @Override
    public Long createChannel(AdChannelSaveReqVO createReqVO) {
        // 插入
        AdChannelDO channel = BeanUtils.toBean(createReqVO, AdChannelDO.class);
        adChannelMapper.insert(channel);
        // 返回
        return channel.getId();
    }

    @Override
    public void updateChannel(AdChannelSaveReqVO updateReqVO) {
        // 校验存在
        validateChannelExists(updateReqVO.getId());
        // 更新
        AdChannelDO updateObj = BeanUtils.toBean(updateReqVO, AdChannelDO.class);
        adChannelMapper.updateById(updateObj);
    }

    @Override
    public void deleteChannel(Long id) {
        // 校验存在
        validateChannelExists(id);
        // 删除
        adChannelMapper.deleteById(id);
    }

    private void validateChannelExists(Long id) {
        if (adChannelMapper.selectById(id) == null) {
            throw exception(CHANNEL_NOT_EXISTS);
        }
    }

    @Override
    public AdChannelDO getChannel(Long id) {
        return adChannelMapper.selectById(id);
    }

    @Override
    public PageResult<AdChannelDO> getChannelPage(AdChannelPageReqVO pageReqVO) {
        return adChannelMapper.selectPage(pageReqVO);
    }


}